package com.yxhl.zoume.common.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.squareup.timessquare.CalendarPickerView;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.busticket.event.ZMTripDateEvent;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.TimeUtils;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;

/**
 * 日历－出行日期选择
 * Created by Xboy on 16/4/5.
 */
public class ZMCalendarActivity extends BaseActivityWithToolBar {
    private static final String TAG = "ZMCalendarActivity";

    @BindView(R.id.calendar_view)
    CalendarPickerView calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set title
        setToolbarTitle("选择出行时间");

        // init calendar
        initCalendar();

        // set calendar listener
        setOnDateSelectedListener();
    }

    /**
     * 初始化Calendar
     * 设定可选范围为最近2个月
     */
    private void initCalendar() {
        final Calendar nextMonth = Calendar.getInstance();
        nextMonth.add(Calendar.DAY_OF_YEAR, 60);

        Date today = new Date();
        calendar.init(today, nextMonth.getTime())
                .inMode(CalendarPickerView.SelectionMode.SINGLE)
                .withSelectedDate(today);
    }

    /**
     * 设置Calendar点击监听
     */
    private void setOnDateSelectedListener() {

        // 日期选择监听
        calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
                LOG.v(TAG, date.toString());

                String tripDate = TimeUtils.dateToString(date);
                RxBus.getInstance().send(new ZMTripDateEvent(tripDate));
                finish();
            }

            @Override
            public void onDateUnselected(Date date) {

            }
        });

        // 非可选日期选择监听
        calendar.setOnInvalidDateSelectedListener(new CalendarPickerView.OnInvalidDateSelectedListener() {
            @Override
            public void onInvalidDateSelected(Date date) {

            }
        });
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_calendar;
    }

//    @Override
//    protected void initButterKnife() {
//        ButterKnife.bind(this);
//    }

    @Override
    protected void initializeInjector() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        ButterKnife.unbind(this);
    }
}
